﻿//-----------------------------------------------------------------------
// <copyright file="FusionStoreSetter.cs">
//     Copyright (c) Nicole Calinoiu. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.Reflection;

namespace Bordecal.FxCop.Sdk.Testing.AppDomainCustomization
{
	[Serializable]
	[SuppressMessage("Microsoft.StyleCop.CSharp.DocumentationRules", "SA1600:ElementsMustBeDocumented",
			Justification = "False positive.")]
	internal sealed class FusionStoreSetter : AppDomainCustomizer<AppDomainSetup>
	{
		private readonly AppDomainSetup _fusionStore;

		internal FusionStoreSetter(AppDomainSetup fusionStore)
		{
			if (fusionStore == null)
			{
				throw new ArgumentNullException("fusionStore");
			}

			this._fusionStore = fusionStore;
		}

		protected override AppDomainSetup CreateDataValue()
		{
			return this._fusionStore;
		}

		protected override void DoCustomizeAppDomain(AppDomain targetDomain, AppDomainSetup data)
		{
			if (targetDomain == null)
			{
				throw new ArgumentNullException("targetDomain");
			}

			MethodInfo setupFusionStoreMethod = typeof(AppDomain).GetMethod("SetupFusionStore", BindingFlags.Instance | BindingFlags.NonPublic);
			setupFusionStoreMethod.Invoke(targetDomain, BindingFlags.Default, null, new object[] { data, targetDomain.SetupInformation }, CultureInfo.InvariantCulture);
		}
	}
}